<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Controller;
class CategoryController extends AdminController {
    public function index(){

        $Category   =   D("Category");
        $info   =   $Category->getTree();
        trace($info,'info');
        //dump($info);
        $this->assign('info',$info);
        $depth  =   array_depth($info)/2;
        $this->assign('depth',$depth);
        trace($depth,'depth');

        $this->display();
    }
    public function add(){

        $Category   =   D("Category");
        $categorys   =   $Category->getTree();
        getDepth($categorys,0,'child','depth');
        $categorys = tree_to_list($categorys,'child');
        $this->assign('categorys',$categorys);


        $pid = $_GET['pid'];

        $this->assign('info',$_GET);
        $this->assign('pannel_title','增加分类');
        $this->display();
    }

    public function edit($id){
        $Category   =   D("Category");
        //全部分类
        $categorys   =   $Category->getTree();
        getDepth($categorys,0,'child','depth');
        $categorys = tree_to_list($categorys,'child');
        $this->assign('categorys',$categorys);
        //分类详细数据
        $info = $Category->getDetail($id);
        $this->assign('info',$info);
        //dump($info);

        $this->assign('pannel_title','编辑分类');
        $this->display();
    }
    public function update(){
        $Category   =   D("Category");
        $success = true;
        $res = $Category->update($_POST);
        if(!$res){
            $success = false;
        }
        if($success){
            $this->success('更新成功',U('Category/index'));
        }else{
            $this->error('错误提示：'.$Category->getError());
        }

    }
    public function getLast($pid){
        $data = array(
            'sort'=>0,
            'title'=>'',
            'pid'=>$pid
        );
        $Category   =   D("Category");
        $res    =   $Category->update($data);
        $this->ajaxReturn(array('id'=>$res['id']));
    }
    public function delete(){
        $Category =   D("Category");
        $id = array_unique((array)I('id',0));
        $res = $Category->del($id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败 '.$Category->getError());
        }
    }

}